<script type="text/javascript">
    var searchForm = {}

    /**
     * 填充搜索表单内的search-type不为normal的数据，表示数值从where中获取
     */
    searchForm.FillValueFromWhere = function () {
        var pathname = window.location.pathname;
        //解析where参数
        var pathArray = pathname.split('/');
        var where = '';
        var j = 0;
        for (var i in pathArray) {
            if (j++ <= 3) {
                continue;
            }

            if (pathArray[i] === "where") {
                where = pathArray[j];
                break;
            }
        }

        var whereArray = where.split('__iceup__');

        $(".form-search").find(':input').each(function () {
            var j = 0;
            var value = '';
            for (var i in whereArray) {
                j++;
                if (whereArray[i] === $(this).attr('name')) {
                    value = whereArray[j];
                    break;
                }
            }
            if (value) {
                $(this).val(decodeURI(whereArray[j]));

            }
        });
    }

    /**
     * 填充搜索表单内的search-type为normal的数据，表示数值从正常的参数中获取
     */
    searchForm.fillValue = function () {
        var pathname = window.location.pathname;
        var pathArray = pathname.split('/');

        $(".form-search").find(':input').each(function () {
            if ($(this).attr('search-type') !== 'normal') {
                return;
            }
            var j = 0;
            var value;
            for (var i in pathArray) {
                if (j++ <= 3) {
                    continue;
                }

                if (pathArray[i] === $(this).attr('name')) {
                    value = pathArray[j];
                    break;
                }
            }
            if (value) {
                $(this).val(decodeURI(pathArray[j]));

            }
        });
    }

    searchForm.FillValueFromWhere();
    searchForm.fillValue();
</script>

<?php
$searchSubmitUri = '';
//
//$i        = 1;
//$requests = array_filter(explode("/", $_SERVER['REQUEST_URI']));
//foreach ($requests as $v) {
//    $searchSubmitUri .= '/' . $v;
//    $i++;
//    if ($i > 3) {
//        break;
//    }
//}

$requests = array_filter(explode("/", $_SERVER['REQUEST_URI']));
foreach ($requests as $v) {
    if ($v == "where") {
        break;
    }
    $searchSubmitUri .= '/' . $v;
}
$searchSubmitUri .= '/';
?>

<script type="text/javascript">
    $(".form-search").submit(function () {
        var searchSubmitUri = '<?php echo $searchSubmitUri; ?>';
        var requestUri = '<?php echo $_SERVER['REQUEST_URI']; ?>';

        var searchValueArray = new Array();
        $(this).find(':input').each(function () {
            var val = $.trim($(this).val());
            if (val !== '') {
                if ($(this).attr('search-type') === 'normal') {
                    searchSubmitUri += $(this).attr('name') + '/' + val + '/';
                } else {
                    searchValueArray.push($(this).attr('name'))
                    searchValueArray.push(val)
                }
            }
        });

        if (searchValueArray.length > 0) {
            searchSubmitUri += 'where/' + searchValueArray.join('__iceup__') + '/';
        }

        if (requestUri !== searchSubmitUri) {
            //如果searchSubmitUri包含/page/\d/，则自动转到第一页
            searchSubmitUri = searchSubmitUri.replace(/\/page\/\d+\//, "/");
            location.href = searchSubmitUri;
        }

        return false;
    });

</script>